#include <iostream>
#include <string.h>
using namespace std;

/*
    浅拷贝:
        概念：（使用的是指针则会出现）
            使用系统默认的拷贝构造函数，创建的新对象会喝原始对象共用同一块堆空间，
            原始对象修改后，新对象也会被修改。即两个指针赋值-->两个指针指向同一块堆空间。
                strcpy(this->name, other.name);导致浅拷贝

    深拷贝：
        概念：
            使用自己定义的拷贝构造函数，解决浅拷贝的问题

*/
class Cat
{
public:
    Cat(int a, char *n, float w)
    {
        this->age = a;
        this->name = new char[strlen(n) + 1];
        strcpy(this->name, n);
        this->weight = w;
    }
    Cat(const Cat &other)
    {
        this->age = other.age;
        this->name = new char[strlen(other.name) + 1];
        strcpy(this->name, other.name);
        this->weight = other.weight;
    }
    void show()
    {
        cout << "----猫的信息-----" << endl;
        cout << "name:" << this->name << endl;
        cout << "age:" << this->age << endl;
        cout << "weight:" << this->weight << endl;
    }
    void setAttr(int a, char *n, float w)
    {
        this->age = a;
        this->name = new char[strlen(n) + 1];
        strcpy(this->name, n);
        this->weight = w;
    }
private:
    int age;
    char *name;
    float weight;

};


int main()
{
    char name[10] = "tom";
    Cat c1(10, name, 5.5);
    Cat c2 = c1;
    c1.show();
    c2.show();
    char newName[10] = "jerry";
    c1.setAttr(11, newName, 6.6);
    c1.show();
    c2.show();


    return 0;
}